package com.scud.product.controller;

import com.scud.common.result.Result;
import com.scud.model.PageResult;
import com.scud.model.product.dto.ManagePageDto;
import com.scud.model.product.pojo.Product;
import com.scud.model.product.vo.ManageProductDetailsVo;
import com.scud.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author vermouth
 */
@RestController()
@RequestMapping("/api/product/manage")
@Slf4j
public class ManagerProductController {


    @Autowired
    private ProductService productService;

    /**
     * 商家商品分页列表查询
     * @param dto:
     * @return Result
     */
    @PostMapping("/page")
    public Result productPage(@RequestBody ManagePageDto dto) {
        log.info("商家商品分页列表查询：{}", dto);
        PageResult<Product> pageResult = productService.manageProductPage(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据商品id获取商品详情
     * @param id:
     * @return Result
     */
    @GetMapping("getDetail/{id}")
    @PreAuthorize("hasAuthority('PRODUCT_DETAIL')")
    public Result getDetail(@PathVariable Long id) {
        log.info("根据商品id获取商品详情：{}", id);
        ManageProductDetailsVo vo = productService.getManageDetails(id);
        return Result.success(vo);
    }

    /**
     * 修改商品信息
     * @param dto:
     * @return Result
     */
    @PutMapping("/updateProduct")
    @PreAuthorize("hasAuthority('SAVE_UPDATE_PRODUCT')")
    public Result updateProduct(@RequestBody ManageProductDetailsVo dto) {
        log.info("修改商品信息：{}", dto);
        productService.updateProduct(dto);
        return Result.success();
    }

    /**
     * 商家根据商品id删除商品
     * @param productId:
     * @return Result
     */
    @DeleteMapping("delete/{productId}")
    @PreAuthorize("hasAuthority('DELETE_PRODUCT')")
    public Result deleteProduct(@PathVariable Long productId) {
        log.info("商家根据商品id删除商品:{}", productId);
        productService.deleteProduct(productId);
        return Result.success();
    }

    /**
     * 修改商品上下架状态
     * @param productId:
     * @param status:
     * @return Result
     */
    @PutMapping("/updateStatus/{productId}/{status}")
    public Result updateStatus(@PathVariable Long productId,
                               @PathVariable Short status) {
        log.info("修改商品上:{}下架状态：{}", productId, status);
        productService.updateStatus(productId, status);
        return Result.success();
    }

}
